package ui;

import bean.Clothes;
import bean.Order;
import bean.OrderItem;
import service.ClothesService;
import service.OrderService;
import service.impl.ClothesServiceImpl;
import service.impl.OrderServiceImpl;
import utils.BusinessException;
import utils.ConsoleTable;
import utils.DateUtils;

import java.util.Date;
import java.util.List;

/*主体界面*/
public class HomeClass extends BaseClass {

    private OrderService orderService;
    private ClothesService clothesService;

    //利用工厂类使其只依赖于接口，而不依赖于接口实现类
    public HomeClass() {
        orderService = (OrderService) beanFactory.getBean("orderService");
        clothesService = (ClothesService) beanFactory.getBean("clothesService");
    }

    public void show() {
        showProducts();
        println("welcome:" + currentUser.getUsername());
        menu();
    }
    //菜单
    private void menu() {
        boolean flag = true;
        while (flag) {
            println(getString("home.function"));
            println(getString("info.select"));
            String select = input.nextLine();
            switch (select) {
                case "0"://0、退出
                    flag = false;
                    println(getString("info.exit"));
                    System.exit(0);
                    break;
                case "1"://1、查询全部订单
                    findOrderList();
                    flag = false;
                    break;
                case "2"://2、查找订单
                    findOrderById();
                    flag = false;
                    break;
                case "3"://3、购买
                    try {
                        buyProducts();
                        flag = false;
                    } catch (BusinessException e) {
                        println(e.getMessage());
                    }
                    break;
                case "4"://显示商品
                    show();
                    break;
                default:
                    println(getString("input.error"));
                    break;
            }
        }
    }

    //展示商品列表
    private void showProducts() {
        List<Clothes> list = clothesService.list();//获取商品列表
        ConsoleTable t = new ConsoleTable(8, true);
        t.appendRow();
        t.appendColumn("id")
                .appendColumn("brand")
                .appendColumn("style")
                .appendColumn("color")
                .appendColumn("size")
                .appendColumn("num")
                .appendColumn("price")
                .appendColumn("description");
        for (Clothes clothes : list) {
            t.appendRow();
            t.appendColumn(clothes.getId())
                    .appendColumn(clothes.getBrand())
                    .appendColumn(clothes.getStyle())
                    .appendColumn(clothes.getColor())
                    .appendColumn(clothes.getSize())
                    .appendColumn(clothes.getNum())
                    .appendColumn(clothes.getPrice())
                    .appendColumn(clothes.getDescription());
        }
        println(t.toString());
    }

    //查询全部订单
    private void findOrderList() {
        List<Order> list = orderService.list();
        for (Order o : list) {
            showOrder(o);
        }
        menu();
    }

    //展示订单列表
    private void showOrder(Order o) {
        print(getString("product.order.oid") + o.getOrderId());
        print("\t" + getString("product.order.createDate") + o.getCreateDate());
        println("\t" + getString("product.order.sum") + o.getSum());
        ConsoleTable t = new ConsoleTable(9, true);
        t.appendRow();
        t.appendColumn("itemId")
                .appendColumn("brand")
                .appendColumn("style")
                .appendColumn("color")
                .appendColumn("size")
                .appendColumn("price")
                .appendColumn("description")
                .appendColumn("shoppingNum")
                .appendColumn("sum");
        for (OrderItem item : o.getOrderItemList()) {
            t.appendRow();
            t.appendColumn(item.getItemId())
                    .appendColumn(item.getClothes().getBrand())
                    .appendColumn(item.getClothes().getStyle())
                    .appendColumn(item.getClothes().getColor())
                    .appendColumn(item.getClothes().getSize())
                    .appendColumn(item.getClothes().getPrice())
                    .appendColumn(item.getClothes().getDescription())
                    .appendColumn(item.getShoppingNum())
                    .appendColumn(item.getSum());
        }
        println(t.toString());
    }

    //通过id查询订单
    private void findOrderById() {
        println(getString("product.order.input.oid"));
        String oid = input.nextLine();
        Order order = orderService.findById(Integer.parseInt(oid));
        if (order != null) {
            showOrder(order);
        } else {
            println(getString("product.order.error"));
        }
        menu();
    }

    //购买商品的方法
    private void buyProducts() throws BusinessException {
        /*
         * 生成订单
         */
        boolean flag = true;
        //订单项的编号
        int count = 1;
        //实例化订单
        Order order = new Order();
        //订单的总金额
        float sum = 0.0f;

        //购买生成订单项，并将订单项放入订单
        while (flag) {
            println(getString("product.input.id"));
            String id = input.nextLine();
            println(getString("product.input.shoppingNum"));
            String shoppingNum = input.nextLine();
            /*---------------------------订单项---------------------------*/
            OrderItem orderItem = new OrderItem();
            Clothes clothes = clothesService.findById(id);//通过商品id找到商品
            //判断购买数量是否大于商品存货量
            int num = Integer.parseInt(shoppingNum);
            if (num > clothes.getNum()) {
                throw new BusinessException("product.num.error");
            }
            //购买后商品存储数量减少(减少商品库存)
            clothes.setNum(clothes.getNum() - num);
            //一条订单明细
            orderItem.setShoppingNum(num);//购买商品数量
            orderItem.setClothes(clothes);//购买商品对象
            orderItem.setSum(num * clothes.getPrice());//订单项总金额
            orderItem.setItemId(count++);//订单项编号自增
            //将订单项放入订单中
            order.getOrderItemList().add(orderItem);
            //订单总金额计算
            sum += orderItem.getSum();

            //购买完成后询问用户
            println(getString("product.buy.continue"));
            String isbuy = input.nextLine();
            switch (isbuy) {
                case "1":
                    flag = true;
                    break;
                case "2":
                    flag = false;
                    break;
                default:
                    flag = false;
                    break;
            }
        }
        /*---------------------------订单---------------------------*/
        //订单创建的时间
        order.setCreateDate(DateUtils.toDate(new Date()));
        //订单的总金额
        order.setSum(sum);
        //订单的用户id
        order.setUserId(currentUser.getId());
        //订单的id
        order.setOrderId(orderService.list().size() + 1);

        //购买商品
        orderService.buyProduct(order);
        //更新商品列表信息
        clothesService.update();
        //重新显示商品列表信息
        show();

    }

}
